<template>
  <div class="reset-pwd">
    <el-form ref="form" :model="user" :rules="rules" label-width="100px">
      <el-form-item label="旧密码" prop="oldPassword">
        <el-input v-model="user.oldPassword" placeholder="请输入旧密码" type="password" show-password />
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword">
        <el-input v-model="user.newPassword" placeholder="请输入新密码" type="password" show-password />
      </el-form-item>
      <el-form-item label="确认密码" prop="confirmPassword">
        <el-input v-model="user.confirmPassword" placeholder="请确认密码" type="password" show-password />
      </el-form-item>
      <el-form-item style="text-align: center;" label-width="0">
        <el-button type="primary" size="mini" @click="submit">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { updateUserPwd } from '@/api/system/user'
import { getSysPassword } from '@/api/system/sys-password'

import { mapGetters } from 'vuex'
import { encrypt } from '@/utils/jsencrypt'
import { passwordValidator } from '@/utils/password'

export default {
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.user.newPassword !== value) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }
    return {
      user: {
        oldPassword: undefined,
        newPassword: undefined,
        confirmPassword: undefined
      },
      // 表单校验
      rules: {
        oldPassword: [
          { required: true, message: '旧密码不能为空', trigger: 'blur' }
        ],
        newPassword: [
          { required: true, message: '新密码不能为空', trigger: 'blur' },
          { required: true, validator: this.passwordValidator, trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '确认密码不能为空', trigger: 'blur' },
          { required: true, validator: equalToPassword, trigger: 'blur' }
        ]
      },
      pwdConfig: null
    }
  },
  computed: {
    ...mapGetters(['name', 'currentUser'])
  },
  mounted() {
    this.loadPwdConfig()
  },
  methods: {
    loadPwdConfig() {
      if (this.currentUser.sysPassword) {
        this.pwdConfig = { ...this.currentUser.sysPassword }
      }
      if (this.pwdConfig == null) {
        getSysPassword().then(res => {
          this.pwdConfig = res.data
        }).catch(err => {
          this.pwdConfig == null
          console.error(err)
        })
      }
    },
    passwordValidator(rule, value, callback, source, options) {
      passwordValidator(this.name, this.pwdConfig, rule, value, callback)
    },
    submit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          updateUserPwd(encrypt(this.user.oldPassword), encrypt(this.user.newPassword)).then(response => {
            this.$modal.msgSuccess('修改成功')
            this.$emit('closeReset', false)
            this.reset()
          })
        }
      })
    },
    reset() {
      this.user = {
        oldPassword: undefined,
        newPassword: undefined,
        confirmPassword: undefined
      }
    },
    close() {
      this.$tab.closePage()
    }
  }
}
</script>
<style lang="scss" scoped>
.reset-pwd {
  padding-top: 15px;
}
</style>
